package com.lida.swagger.controller;

import com.lida.swagger.pojo.ResultData;
import com.lida.swagger.pojo.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * author: lida
 * create time: 2020-09-02 17:53
 * description: Implicit:含蓄的
 */
@RestController
@Api(value = "用户", tags = {"用户模块"})     //设置模块的名字
public class SwaggerController {

    /**
     * 登录案例一
     */
    @RequestMapping(value = "login_v1", method = RequestMethod.GET)
    @ApiOperation(value = "用户登录接口_V1", notes = "用户名及密码不能为空")  //描述这个接口
    @ApiImplicitParams({    //请求参数的说明
            @ApiImplicitParam(name = "uname", value = "用户名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = true, paramType = "query", dataType = "String")
    })
    @ApiResponses({     //响应参数的说明
            @ApiResponse(code = 1, message = "请求成功", response = ResultData.class),
            @ApiResponse(code = 0, message = "请求失败", response = ResultData.class)
    })
    public ResultData<User> login_v1(String uname, String pwd) {
        ResultData<User> resultData = new ResultData<>();
        resultData.setCode(1);
        resultData.setMsg("请求成功");
        resultData.setT(new User(1, uname, pwd));
        return resultData;
    }


    /**
     * 登录案例二：
     * 如果传递是 json 格式转换成 java 对象，那么下面不需要对参数进行描述
     */
    @RequestMapping(value = "login_v2", method = RequestMethod.POST)
    @ApiOperation(value = "用户登录接口_V2", notes = "用户名及密码不能为空")
    @ApiResponses({     //响应参数的说明
            @ApiResponse(code = 1, message = "请求成功", response = ResultData.class),
            @ApiResponse(code = 0, message = "请求失败", response = ResultData.class)
    })
    public ResultData<User> login_v2(@RequestBody User user) {
        ResultData<User> resultData = new ResultData<>();
        resultData.setCode(1);
        resultData.setMsg("请求成功");
        resultData.setT(new User(1, user.getUname(), user.getPwd()));
        return resultData;
    }


    /**
     * 案例三:通过id 找用户（一个参数的情况）
     */
    @RequestMapping(value = "findUserByid/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过用户 id 找用户", notes = "用户 id 不能为空")
    @ApiImplicitParam(name = "id", value = "用户 id", required = true, paramType = "path", dataType = "int")
    @ApiResponses({     //响应参数的说明
            @ApiResponse(code = 1, message = "请求成功", response = ResultData.class),
            @ApiResponse(code = 0, message = "请求失败", response = ResultData.class)
    })
    public ResultData<User> findUserById(@PathVariable("id") Integer id) {
        ResultData<User> resultData = new ResultData<>();
        resultData.setCode(1);
        resultData.setMsg("请求成功");
        resultData.setT(new User(id, "linda", "qwe"));
        return resultData;
    }


    /**
     * 案例四：测试将将 token 放到请求头中
     */
    @GetMapping(value = "token")
    @ApiOperation(value = "测试 token", notes = "token 不为空")
    @ApiImplicitParam(name = "token", value = "用户 token", required = true, paramType = "header", dataType = "String")
    @ApiResponses({     //响应参数的说明
            @ApiResponse(code = 1, message = "请求成功", response = ResultData.class),
            @ApiResponse(code = 0, message = "请求失败", response = ResultData.class)
    })
    public ResultData<String> testToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        ResultData<String> resultData = new ResultData<>();
        resultData.setCode(1);
        resultData.setMsg("请求成功");
        resultData.setT(token);
        return resultData;
    }


    /**
     * 案例五：传递 token 和普通的参数的问题
     */
    @GetMapping("tokenAndNomal")
    @ApiOperation(value = "测试传递 token 和普通的参数", notes = "所有参数不为空")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户 token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "uname", value = "用户名", required = true, paramType = "query", dataType = "String"),
    })
    @ApiResponses({     //响应参数的说明
            @ApiResponse(code = 1, message = "请求成功", response = ResultData.class),
            @ApiResponse(code = 0, message = "请求失败", response = ResultData.class)
    })
    public ResultData<User> testTokenAndNomalParam(HttpServletRequest request, String uname) {
        String token = request.getHeader("token");
        System.out.println("token 为：" + token);
        ResultData<User> resultData = new ResultData<>();
        resultData.setCode(1);
        resultData.setMsg("请求成功");
        resultData.setT(new User(1, uname, "qwe"));
        return resultData;
    }

}
